package itsm.isperp.module.utils;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.dialect.DialectFactory;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.data.jdbc.support.DatabaseType;
import org.springframework.jdbc.core.JdbcTemplate;

public class ChangeConfCiProprety {
	
	public ChangeConfCiProprety()
	{
	}
	public static void UpdateChangeConCi(String changeId) {
		System.out.println("---------------------BeginUpdateChangeCon_Ci-------------------at:"+new Date(System.currentTimeMillis()));
        
		// 根据ChangeId 获取需要更新的资产
		String sql_getChangeCi = "SELECT change_id,ci_id FROM chg_change_configuration WHERE  change_id=? GROUP BY change_id,ci_id";

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		List<Map<String, Object>> ciList = jdbcTemplate.queryForList(sql_getChangeCi,
				changeId);
		String modifierDate="";
		String uuId=HibernateUUIDGenerator.generate();
		String dbUUID="";
		if (DialectFactory.getDatabaseType() == DatabaseType.MYSQL) {
			modifierDate="now()"; 
			dbUUID="uuid()";
		} else {
			modifierDate="sysdate";
			dbUUID="sys_guid()";
		} 

		for (int i = 0; i < ciList.size(); i++) {
			Map<String, Object> rCi = ciList.get(i);
			String ciid = (String) rCi.get("ci_id");
			String modifierName="";
			String modifier="";
			//更新基本属性
			String sql_ci = "SELECT *  FROM chg_change_configuration WHERE  change_id=? and ci_id=? and is_category_proprety='0' and (status<>'0' or status is null)";
			List<Map<String, Object>> sql_ci_list = jdbcTemplate.queryForList(sql_ci,
					changeId, ciid);
			if (sql_ci_list.size() != 0) {
				// 更新资产基本属性
				String updateColumn = "";
				String insertDateLog1="";
				//更新属性日志
				
				modifierName=(String)sql_ci_list.get(0).get("modifier_name");
				modifier=(String)sql_ci_list.get(0).get("modifier");
				String tDescription="";
				// 构造需要更新的列
				for (int j = 0; j < sql_ci_list.size(); j++) {
					
					String columnkey_=itsm.isperp.framework.utils.StringUtils
							.underscore((String)sql_ci_list.get(j).get("columnkey"));
					if(sql_ci_list.get(j).get("columnvalue")!=null)
					{
						updateColumn += columnkey_ + "='"
								+ (String)sql_ci_list.get(j).get("columnvalue") + "',";
					}else
					{
						updateColumn += columnkey_ + "=null,";
					}
					String oldValue="null";
					if(sql_ci_list.get(j).get("columnvalue_before")!=null)
					   oldValue=sql_ci_list.get(j).get("columnvalue_before").toString();
					String changeColumnName=sql_ci_list.get(j).get("columnname").toString();
					tDescription+="【"+changeColumnName+"】："+oldValue+"->"+sql_ci_list.get(j).get("columnvalue").toString()+";";
					
				}
				insertDateLog1+="insert into db_entity_datalog(id,business_id,table_name,change_id,description,creator,creator_name,create_date) " 
						+" values('"+uuId+"','"+ciid+"','conf_ci','"+changeId+"','"+tDescription+"','"+modifier+"','"+modifierName+"',"+modifierDate+")";
				
				String sql_ci_update = "update conf_ci set " + updateColumn
						+ "modifier_name='"+modifierName+"',modifier='"+modifier+"',modify_date="+modifierDate+" where id='" + ciid+"'";
				jdbcTemplate.update(sql_ci_update);
				jdbcTemplate.execute(insertDateLog1);
			}
			//更新扩展属性
			String sql_ci_expro = "SELECT *  FROM chg_change_configuration WHERE  change_id=? and ci_id=? and is_category_proprety='1' and (status<>'0' or status is null)";
			List<Map<String, Object>> sql_ci_expro_list = jdbcTemplate.queryForList(sql_ci_expro,
					changeId, ciid);
			if (sql_ci_expro_list.size() != 0) {
				
				modifierName=(String)sql_ci_expro_list.get(0).get("modifier_name");
				modifier=(String)sql_ci_expro_list.get(0).get("modifier");
				String insertDateLog2="";
				String tDescription="";
				for (int j = 0; j < sql_ci_expro_list.size(); j++) {
					String confProtyid=((String)sql_ci_expro_list.get(j).get("columnkey"));
					String sql_ci_expro_update="";
					if(sql_ci_expro_list.get(j).get("columnvalue")==null)
					{
						sql_ci_expro_update="update conf_ci_property set value=null,"+ "modifier_name='"+modifierName+"',modifier='"+modifier+"',modify_date="+modifierDate+"  where conf_category_property_id='"+confProtyid+"' and conf_ci_id='"+ciid+"'";
					}else
					{
						String columnvalue=(String)sql_ci_expro_list.get(j).get("columnvalue");
						sql_ci_expro_update="update conf_ci_property set value='"+columnvalue+"',"+ "modifier_name='"+modifierName+"',modifier='"+modifier+"',modify_date="+modifierDate+"  where conf_category_property_id='"+confProtyid+"' and conf_ci_id='"+ciid+"'";
					}
					
					jdbcTemplate.update(sql_ci_expro_update);
					String oldValue="null";
					if(sql_ci_expro_list.get(j).get("columnvalue_before")!=null)
					   oldValue=sql_ci_expro_list.get(j).get("columnvalue_before").toString();
					String changeColumnName=sql_ci_expro_list.get(j).get("columnname").toString();
					tDescription+="【"+changeColumnName+"】："+oldValue+"->"+sql_ci_expro_list.get(j).get("columnvalue").toString()+";";
					
				}
				insertDateLog2="insert into conf_ci_property_datalog(id,business_id,change_id,datalog_id,description,creator,creator_name,create_date) " 
						+" values("+dbUUID+",'"+ciid+"','"+changeId+"','"+uuId+"','"+tDescription+"','"+modifier+"','"+modifierName+"',"+modifierDate+")";
				jdbcTemplate.execute(insertDateLog2);
			}else
			{
				String insertDateLog2="insert into conf_ci_property_datalog(id,business_id,change_id,datalog_id,description,creator,creator_name,create_date) " 
						+" values("+dbUUID+",'"+ciid+"','"+changeId+"','"+uuId+"','','"+modifier+"','"+modifierName+"',"+modifierDate+")";
				jdbcTemplate.execute(insertDateLog2);
			}
			//更新属性变更日志 
			
		}
		System.out.println("---------------------EndUpdateChangeCon_Ci---------------------at:"+new Date(System.currentTimeMillis()));
	}
}
